﻿using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuanLyThongTinNhanKhau.GUI
{
    public partial class TienAnTienSu : Form
    {
        private GUI.NhanKhau nhanKhau;
        static String maNhanKhau;
        model.BO.TienAnTienSuBO tienAnTienSuBO;

        public TienAnTienSu(GUI.NhanKhau f, String maNK)
        {
            InitializeComponent();
            this.nhanKhau = f;
            maNhanKhau = maNK;
        }

        private void TienAnTienSu_Load(object sender, EventArgs e)
        {
            tienAnTienSuBO = new model.BO.TienAnTienSuBO(this, nhanKhau);

            this.tbMaNhanKhau.Text = maNhanKhau;

            tienAnTienSuBO.loadDefault(maNhanKhau);
            tienAnTienSuBO.loadTable(maNhanKhau);

            //Visible Panel Top.
            panelTop.Visible = false;
            this.gridControlTATS.Location = new System.Drawing.Point(2, 2);
            this.gridControlTATS.Size = new System.Drawing.Size(987, 630);
        }

        private void toolStripMenuItemThemMoi_Click(object sender, EventArgs e)
        {
            panelTop.Visible = true;
            this.gridControlTATS.Location = new System.Drawing.Point(2, 262);
            this.gridControlTATS.Size = new System.Drawing.Size(987, 370);
            
            tienAnTienSuBO.genarateMaTATS();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            tienAnTienSuBO.btnLuu();
        }

        private void gridViewTATS_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            GridView view = sender as GridView;
            GridHitInfo hitInfo = view.CalcHitInfo(e.Point);

            if (hitInfo.InRow)
            {
                view.FocusedRowHandle = hitInfo.RowHandle;
                this.contextMenuStripTATS.Show(view.GridControl, e.Point);
            }
            else
            {
                this.contextMenuStripTATS.Show(view.GridControl, e.Point);
            }
        }

        private void sửaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.gridViewTATS.SelectedRowsCount > 0)
            {
                panelTop.Visible = true;
                this.gridControlTATS.Location = new System.Drawing.Point(2, 262);
                this.gridControlTATS.Size = new System.Drawing.Size(987, 370);

                tbMaTATS.Text = gridViewTATS.GetRowCellValue(gridViewTATS.FocusedRowHandle, "MaTienAnTienSu").ToString();
                dateTimeNgayViPham.Text = gridViewTATS.GetRowCellValue(gridViewTATS.FocusedRowHandle, "NgayViPham").ToString();
                tbNoiDung.Text = gridViewTATS.GetRowCellValue(gridViewTATS.FocusedRowHandle, "NoiDung").ToString();
                tbHinhThucXuLy.Text = gridViewTATS.GetRowCellValue(gridViewTATS.FocusedRowHandle, "HinhThucXuLy").ToString();
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            //Visible Panel Top.
            panelTop.Visible = false;
            this.gridControlTATS.Location = new System.Drawing.Point(2, 2);
            this.gridControlTATS.Size = new System.Drawing.Size(987, 630);
        }

        private void xóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.gridViewTATS.SelectedRowsCount > 0)
            {
                String maTATS = gridViewTATS.GetRowCellValue(gridViewTATS.FocusedRowHandle, "MaTienAnTienSu").ToString();
                String maNhanKhau = gridViewTATS.GetRowCellValue(gridViewTATS.FocusedRowHandle, "MaNhanKhau").ToString();

                DialogResult result = MessageBox.Show("Bạn có muốn xóa Mã Tạm Trú: " + maTATS + "???", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                //If Yes
                if (result == DialogResult.Yes)
                {
                    tienAnTienSuBO.deleteRow(maTATS, maNhanKhau);
                }
            }
        }
    }
}
